﻿//Copyright © 2012  Daniel Lopez

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace NASM_Develop_IDE
{
    [Obsolete("This is class was originally supposed to be the control for NASM editing but since Scintilla is far a better control and allows for better configuration. This class is depecrated but will remain her until further notice.", true)]
    public partial class NASMEdit : UserControl
    {
        
        public NASMEdit(string author = "Mr. No Jobs")
        {
           
            InitializeComponent();
            editor.WordWrap = false;
            editor.Font = new Font("Consolas", 10, editor.Font.Style, GraphicsUnit.Point);
            editor.AcceptsTab = true;
            editor.Focus();
            editor.Text = "; Author: " + author + "\n;Created On: " + DateTime.Now.ToString();
        }

        private void editor_KeyUp(object sender, KeyEventArgs e)
        {
            if (editor.Text != string.Empty && (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space || e.KeyCode == Keys.Tab))
            {
                
                Regex registers = new Regex(@"\bmov\b|\beax\b|\bebx\b|\becx\b|\bedx\b|\bax\b|\bbx\b|\bcx\b|\bdx\b|\bal\b|\bah\b|\besp\b|\bcmp\b|\bjmp\b|\bje\b|\bsection .text\b|\bsection .data\b|\bsection .bss\b|\bdword\b|\bdb\b|\bdd\b|\blodsb\b|\bpush\b|\bpop\b|\bret\b|\bpusha\b|\bpopa\b|\besi\b|\bcall\b|\bglobal\b|\bextern\b|\bxor\b");
                MatchCollection matches = registers.Matches(editor.Text);
                foreach (Match match in matches)
                {
                    int previousIndex = editor.SelectionStart;

                    editor.Select(match.Index, match.Length);
                    if (editor.SelectionColor != Color.Blue)
                    {
                        editor.SelectionColor = Color.Blue;
                        editor.SelectionStart = previousIndex;
                        editor.SelectionColor = Color.Black;
                    }
                    else
                    {
                        editor.SelectionStart = previousIndex;
                    }
                    //List<string> lines = editor.Lines.ToList();
                    //List<string> commentedLines =  lines.FindAll((string line) => (line.Contains(";")));
                    //int totalLength = 0;
                    //commentedLines.ForEach(delegate(string line)
                    //{
                    //    int index = line.IndexOf(';') + (totalLength - 1);
                    //    if (index >= 0)
                    //    {
                    //        int length = line.Length - (index + 1);
                    //        editor.Select(index, length + 1);
                    //        if (editor.SelectionColor != Color.DarkGreen)
                    //        {
                    //            editor.SelectionColor = Color.DarkGreen;
                    //            editor.SelectionStart = previousIndex;
                    //            editor.SelectionColor = Color.Black;
                    //        }
                    //        editor.SelectionStart = previousIndex;
                    //    }
                    //    totalLength += line.Length;
                    //});
                }
                editor.Focus();
            }
        }
        public void setError(int line) 
        {
            if (!isWarningOrError(line))
            {
                if (line <= 0) { throw new ArgumentException(); }
                List<string> lines = editor.Lines.ToList();
                string str = lines[line - 1];
                int index = editor.Text.IndexOf(str);
                int length = str.Length;
                int previousIndex = editor.SelectionStart;
                editor.Select(index, length);
                editor.SelectionFont = new Font(editor.SelectionFont, FontStyle.Underline);
                editor.SelectionColor = Color.Red;
            }
        }
        public void disableError(int line)
        {
            if (isWarningOrError(line))
            {
                if (line <= 0) { throw new ArgumentException(); }
                List<string> lines = editor.Lines.ToList();
                string str = lines[line - 1];
                int index = editor.Text.IndexOf(str);
                int length = str.Length;
                int previousIndex = editor.SelectionStart;
                editor.Select(index, length);
                editor.SelectionFont = new Font(editor.SelectionFont, FontStyle.Regular);
                editor.SelectionStart = previousIndex;
            }
        }
       
        public void setWarning(int line)
        {
            if (!isWarningOrError(line))
            {
                if (line <= 0) { throw new ArgumentException(); }
                List<string> lines = editor.Lines.ToList();
                string str = lines[line - 1];
                int index = editor.Text.IndexOf(str);
                int length = str.Length;
                int previousIndex = editor.SelectionStart;
                editor.Select(index, length);
                editor.SelectionFont = new Font(editor.SelectionFont, FontStyle.Underline);
                editor.SelectionColor = Color.Green;
            }
        }
        public void disableWarning(int line)
        {
            if (isWarningOrError(line))
            {
                if (line <= 0) { throw new ArgumentException(); }
                List<string> lines = editor.Lines.ToList();
                string str = lines[line - 1];
                int index = editor.Text.IndexOf(str);
                int length = str.Length;
                int previousIndex = editor.SelectionStart;
                editor.Select(index, length);
                editor.SelectionFont = new Font(editor.SelectionFont, FontStyle.Regular);
                editor.SelectionStart = previousIndex;
                editor.SelectionColor = Color.Black;
            }
        }
        private bool isWarningOrError(int line)
        {
            if (line <= 0) { throw new ArgumentException(); }
            List<string> lines = editor.Lines.ToList();
            string str = lines[line - 1];
            int index = editor.Text.IndexOf(str);
            int length = str.Length;
            int previousIndex = editor.SelectionStart;
            editor.Select(index, length);
            if (editor.SelectionFont.Underline && (editor.SelectionColor == Color.Red || editor.SelectionColor == Color.Green))
            {
                editor.SelectionStart = previousIndex;
                return true;
            }
            else
            {
                return false;
            }
        }
        public void ChangeDefaultText(string newText)
        {
            editor.Text = newText;
        }
        public void ForceHighlight()
        {
            editor_KeyUp(this, new KeyEventArgs(Keys.Enter));
        }
    }
}
